import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { Location } from '@angular/common';
import { UtilService } from './util.service';
declare var io: any;
declare var $: any;

@Injectable()
export class appService {
    private headers = new Headers({ 'Content-Type': 'application/json' });
    constructor(private http: Http, private location: Location,private UtilService:UtilService) { }

    postcanvastrain(body) {
        const url = `${this.UtilService.canvasurl}/cxhb/trainning/addTrainning`;
        return this.http.post(url, body, { headers: this.headers })
          .toPromise()
          .then(res => { return res.json() })
        
      }
    GetAllCanvasList(): Promise<any> {
        return this.http.get(`${this.UtilService.canvasurl}/cxhb/trainning/getTraining?m=0&n=1000`).toPromise().then(res => {
            return res.json();
        })
    }
    GetCanvasList(n: number, m: number): Promise<any> {
        return this.http.get(`${this.UtilService.canvasurl}/cxhb/trainning/getTraining?m=${n}&n=${m}`).toPromise().then(res => {
            return res.json();
        })
    }
    getPointer(trainguid): Promise<any> {
        return this.http.get(`${this.UtilService.canvasurl}/cxhb/trainning/getPointer/${trainguid}`).toPromise().then(res => {
            return res.json();
        })
    }
    postnote(data): Promise<any> {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/postnote`;
        return this.http.post(url, data, { headers: this.headers })
            .toPromise()
            .then(res => { return res.json() })
    }
    putnote(data):Promise<any> {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/putnote`;
        return this.http.post(url, data, { headers: this.headers })
            .toPromise()
            .then(res => { return res.json() })
    }
    getnum(FK_CanvasGroupGuid, PK_AnchorGuid): Promise<any> {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/getnum/${FK_CanvasGroupGuid}?PK_AnchorGuid=${PK_AnchorGuid}`;
        return this.http.get(url)
            .toPromise()
            .then(res => { return res.json() })
    }
    getnote(FK_CanvasGroupGuid, PK_AnchorGuid): Promise<any> {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/getnote/${FK_CanvasGroupGuid}?PK_AnchorGuid=${PK_AnchorGuid}`;
        return this.http.get(url)
            .toPromise()
            .then(res => { return res.json() })
    }
    getallnote(FK_CanvasGroupGuid): Promise<any> {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/getallnote/${FK_CanvasGroupGuid}`;
        return this.http.get(url)
            .toPromise()
            .then(res => { return res.json() })
    }
    delnote(PK_NoteGuid): Promise<any> {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/delnote/${PK_NoteGuid}`;
        return this.http.delete(url).toPromise().then(res => { return res.json() })
    }
    getcanvasguid(PK_CanvasGroupGuid) {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/getcanvasguid/${PK_CanvasGroupGuid}`;
        return this.http.get(url)
            .toPromise()
            .then(re => { return re.json()[0]; })
    }
    //获取canvas 的详细
    getcanvas(canvasid): Promise<any> {
        const url = `${this.UtilService.canvasurl}/cxhb/canvas/canvasInfo/${canvasid}`;
        return this.http.get(url)
            .toPromise()
            .then(re => { return re.json()[0]; })
    }
    trainmsg(PK_TrainningGuid): Promise<any> {
        const url = `${this.UtilService.canvasurl}/cxhb/trainning/trainmsg/${PK_TrainningGuid}`;
        return this.http.get(url)
            .toPromise()
            .then(re => { return re.json(); })
    }
    search(searchText): Promise<any> {
        const url = `${this.UtilService.canvasurl}/cxhb/trainning/multiSel?searchText=${searchText}`;
        return this.http.get(url)
            .toPromise()
            .then(re => { return re.json(); })
    }
    getcanvasgroup(PK_TrainningGuid, m, n) {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/getCanvasgroup?FK_TrainningGuid=${PK_TrainningGuid}&m=${m}&n=${n}`;
        return this.http.get(url)
            .toPromise()
            .then(re => { return re.json(); })
    }
    getNoteNum(PK_CanvasGroupGuid) {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/getallnote/${PK_CanvasGroupGuid}`;
        return this.http.get(url)
            .toPromise()
            .then(re => { return re.json().length })
    }
    postNewCanvasGroup(data, PK_TrainningGuid): Promise<any> {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/postNewCanvasGroup`;
        let postdata = {
            Name: data.Name,
            Password:data.Password,
            FK_TrainningGuid: PK_TrainningGuid
        }
        return this.http.post(url, postdata, { headers: this.headers })
            .toPromise()
            .then(res => { return res.json() })
    }
    getDelNote(PK_CanvasGroupGuid) {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/getDelNote/${PK_CanvasGroupGuid}`;
        return this.http.get(url)
            .toPromise()
            .then(re => { return re.json() })
    }
    recovery(PK_NoteGuid) {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/recovery/${PK_NoteGuid}`;
        return this.http.get(url)
            .toPromise()
            .then(re => { return re.json() })
    }
    getonenote(PK_NoteGuid) {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/getonenote/${PK_NoteGuid}`;
        return this.http.get(url)
            .toPromise()
            .then(re => { return re.json() })
    }
    save(href) {
        const url = `${this.UtilService.canvasurl}/cxhb/canvasgroup/save?href=${href}`;
        return this.http.get(url)
            .toPromise()
            .then(re => { return this.UtilService.canvasurl + re.json().url })
    }
        //16进制颜色转rgba
    RGB2RGBA(rgb_color, alp) {
        //注：rgb_color的格式为#FFFFFFF
        let r = parseInt(`0x${rgb_color.substr(1, 2)}`);
        let g = parseInt(`0x${rgb_color.substr(3, 2)}`);
        let b = parseInt(`0x${rgb_color.substr(5, 2)}`);
        let a = alp;//透明度
        return `rgba(${r},${g},${b},${a})`;
    }

    //生成guid    
    GenGuid() {
        let guid = "";
        for (let i = 1; i <= 32; i++) {
            let n = Math.floor(Math.random() * 16.0).toString(16);
            guid += n;
            if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                guid += "-";
        }
        return guid;
    }


    lpplist(data){
        const url = `${this.UtilService.canvasurl}/cxhb/trainning/lpplist`;
        return this.http.post(url,data)
            .toPromise()
            .then(re => { return re.json() })
    }

    getThisUser(PK_UserGuid){
        const url = `${this.UtilService.accountUrl}/open/getThisUser?PK_UserGuid=${PK_UserGuid}`;
        return this.http.get(url)
            .toPromise()
            .then(re => { return re.json() })
    }

    deltrain(data){
        const url = `${this.UtilService.canvasurl}/cxhb/trainning/deltrain`;
        return this.http.put(url,data)
            .toPromise()
            .then(re => { return re.json() })
    }
    edittrain(data){
        const url = `${this.UtilService.canvasurl}/cxhb/trainning/edittrain`;
        return this.http.put(url,data)
            .toPromise()
            .then(re => { return re.json() })
    }
    note(data){
        const url = `${this.UtilService.canvasurl}/score/note`;
        return this.http.post(url,data)
            .toPromise()
            .then(re => { return re.json() })
    }
    noteByStudent(data){
        const url = `${this.UtilService.canvasurl}/score/noteByStudent`;
        return this.http.post(url,data)
            .toPromise()
            .then(re => { return re.json() })
    }
}